// pages/index/index.js
import {
  get
} from '../../utils/http'

Page({
  /**
   * 页面的初始数据
   */
  data: {
    statusHeight: getApp().globalData.statusHight,
    triggeredRefresh: false,
    pageOpt: {
      pageNum: 1,
      pageSize: 5,
      userSearchContent: ""
    },
    pageTotal: 0,
    rows:[],
    loadMoreText: "点击加载更多",
    showReset: false,
    searchValue: "",
    loading: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getTabBar().setData({
      current: 'index'
    })
    this.init()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getTabBar().setData({
      current: 'index'
    })
    this.init()
  },

  onRefresh() {
    this.pullData()
  },

  onLoadMore(e){
    this.pullData()
  },

  onConfirm(e){
    console.log(e.detail.value)
    const {value} = e.detail
    this.setData({
      "pageOpt.pageNum": 1,
      "pageOpt.userSearchContent": value
    })
    this.pullData(true)
  },

  init(){
    console.log("index init.")
    this.setData({"pageOpt.pageNum": 1,"pageOpt.userSearchContent": ''})
    this.pullData(true)
  },

  toDetail(e){
    const {detail} = e.currentTarget.dataset
    // console.log(e.currentTarget.dataset.detail)
    wx.navigateTo({
      url: `/pages/index/detail?id=${detail.id}`,
      success: function(params) {
        params.eventChannel.emit("indexData",detail)
      }
    })
  },

  onSearchChange(e){
    if(e.detail.value.length >0){
      this.setData({
        showReset:true
      })
    }else{
      this.setData({
        showReset:false
      })
    }
  },

  onResetSearch(){
    if(!this.data.showReset){
      return
    }
    this.setData({
      searchValue: "",
      showReset:false
    })
    this.init()
  },

  pullData(init=false) {
    if(this.data.loading){
      return
    }
    this.setData({
      loading: true,
      loadMoreText: '数据加载中'
    })
    const that = this;
    get("/user/industryTrends/list", {
      ...that.data.pageOpt
    }).then(res => {
      const rows = res.rows || []
      let total = res.total || 0
      if(total != 0){
        total = Math.ceil(total / that.data.pageOpt.pageSize)
      }

      if(that.data.pageOpt.pageNum > total){
        if(that.data.rows.length == rows.length){
          wx.showToast({
            title: '没有更多数据了',
            icon: 'none'
          })
          that.setData({
            triggeredRefresh: false,
            pageTotal: total,
            loading:false,
            loadMoreText: "没有更多数据了"
          })
          return
        }else{
          
          init = true
        }
      }

      if(rows.length > 0){
         rows.forEach(row => {
          let imagestr = row.coverImage
          if(imagestr && imagestr.includes(",")){
            row.coverImage = imagestr.split(",")
            return
          }
          if(imagestr){
            row.coverImage = [imagestr]
            return
          }
          row.coverImage = [] 
        });
      }
      if(that.data.rows.length > 0 && !init){
        rows.push(...that.data.rows)
      }
      let pageNum = that.data.pageOpt.pageNum + 1
      let text = '点击加载更多'
      if(rows.length < that.data.pageOpt.pageSize){
        text = "没有更多数据了"
      }
      that.setData({
        triggeredRefresh: false,
        "pageOpt.pageNum": pageNum,
        pageTotal: total,
        loadMoreText: text,
        loading:false,
        rows
      })
    }).catch(err => {
      wx.showToast({
        title: '网络异常,请稍后再试',
        icon: 'none'
      })
      that.setData({
        triggeredRefresh: false,
        loading:false,
        loadMoreText:'网络异常'
      })
    })
  },
})